package com.feihao.zhou.dao;

import com.feihao.zhou.bean.Person;
import com.feihao.zhou.bean.Student;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.junit.Test;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

public class Demo01 {

    /**
     * 删除学生：
     */
    @Test
    public void test6() throws IOException {
        //加载核心配置文件：
        InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");

        //获取sqlSession工厂对象：
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);
        //获取sqlsession对象
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //执行sql：
        int delete = sqlSession.delete("StudentMapper.delete1", 6);

        //提交事务！！！！！！！！！！！！！！！！
        sqlSession.commit();
        System.out.println(delete);

        //释放资源：
        is.close();
        sqlSession.close();
    }

    /**
     * 修改学生：
     */
    @Test
    public void test5() throws IOException {
        InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");

        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

        SqlSession sqlSession = sqlSessionFactory.openSession(true);

        Student student = new Student(5, "小虎", 25, new Date(88582488566L));
        int update = sqlSession.update("StudentMapper.update1", student);
        System.out.println(update);

        is.close();
        sqlSession.close();

    }


    /**
     * 新增2
     *
     * @throws IOException
     */
    @Test
    public void test4() throws IOException {
        Date date = new Date();
        System.out.println(date);
        Date date1 = new Date(48393455934L);
        System.out.println(date1);
        //1.加载核心配置文件：
        InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");

        //2.获取sqlsessionfactory对象：
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

        //获取sqlsession对象：
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //执行配置文件的sql语句：
        Student student = new Student(6, "阿六", 26, new Date());
        int insert = sqlSession.insert("StudentMapper.insert", student);

        //手动提交事务：
        sqlSession.commit();
        //打印输出：
        System.out.println(insert);

        //释放资源：
        is.close();
        sqlSession.close();
    }


    /**
     * 新增：
     */
    @Test
    public void test3() throws IOException {
        //1.加载核心配置文件：
        InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");

        //2.获取sqlsessionfactory对象：
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

        //获取sqlsession对象：
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //执行配置文件的sql语句：
        Student student = new Student(5, "小了", 25, new Date());
        int insert = sqlSession.insert("StudentMapper.insert", student);

        //手动提交事务：
        sqlSession.commit();
        //打印输出：
        System.out.println(insert);

        //释放资源：
        is.close();
        sqlSession.close();
    }

    /**
     * 根据id查询：
     */
    @Test
    public void test2() throws IOException {
        //1.加载核心配置文件：
        InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");

        //2.获取sqlsessionfactory对象：
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

        //获取sqlsession对象：
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //执行配置文件的sql语句：
        Student student = sqlSession.selectOne("StudentMapper.selectById", 2);

        //打印输出：
        System.out.println(student);

        //释放资源：
        is.close();
        sqlSession.close();
    }

    @Test
    public void test1() throws IOException {
        //1.加载核心配置文件：
        InputStream is = Resources.getResourceAsStream("MybatisConfig.xml");

        //2.获取sqlsession工厂对象：
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(is);

        //通过工厂对象，获取sqlsession对象：
        SqlSession sqlSession = sqlSessionFactory.openSession();

        //执行映射配置文件中的sql语句，接收结果：
        List<Student> list = sqlSession.selectList("StudentMapper.selectAll");

        //遍历打印：
        for (Student student : list) {
            System.out.println(student);
        }

        //施放资源：
        is.close();
        sqlSession.close();

    }


}
